﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Framework.Data;
using Dal.Entities;

namespace Web.Controllers
{
    public class IndexController : Controller
    {
        //
        // GET: /Index/

        public ActionResult Index()
        {
            var userid = Common.GetUserID();
            var doctype = Common.DocType();
            bool isAdmin = Common.Admin();
            var rep = RepositoryFactory.GetRepository();
            var listTmp = from doc in rep.GetQuery<Doc>()
                          join user in rep.GetQuery<User>()
                          on doc.UserID equals user.ID
                          where doc.DocType == doctype && doc.StatusValid == 1
                          orderby doc.DocID descending
                          select new HomeDoc { Doc = doc, UserName = user.UserName, UserID = user.ID };
            if (!isAdmin)
                listTmp = listTmp.Where(i => i.UserID == userid);
            
            var list = listTmp.ToList();

            ViewBag.NotComplete = list.Where(i => i.Doc.StatusComplete == 0).ToList();
            ViewBag.NotAudit = list.Where(i => i.Doc.StatusAudit == 0).ToList();
            ViewBag.Question = list.Where(i => i.Doc.StatusQuestion == 1).ToList();
            ViewBag.Suifang = list.OrderByDescending(i => i.Doc.DocID).Take(10).ToList();//最新10条
            return View();
        }

        public class HomeDoc
        {
            public Doc Doc { get; set; }
            public string UserName { get; set; }
            public int UserID { get; set; }
        }
    }
}
